<?php 
if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Businesses extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
		}
		
		function index($query_id=0, $order_by='created_date', $sort_order='asc', $current_page=0){
			$search_by='';
			$search_by_value='';
				if($query_id == '0'){		
					//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					//echo '<pre>';print_r($search_by_value);exit;
					
					}
				}
			$results_per_page = 3;
		
		
			$url = CURL_URL;
			$data = array('www-command'=>'business-all_businesses', 'current_page'=>$current_page, 'results_per_page'=>$results_per_page, 'order_by'=>$order_by, 'sort_order'=>$sort_order,'search_by'=>$search_by,'search_by_value'=>$search_by_value);
			
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response= json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response);exit;
			if($response->message !== 'No business'){
			$data['business_all_businesses'] = $response;
			
			
			curl_close($ch);
		
			$this->load->library('pagination');			
			$config['base_url'] = base_url().'admin/businesses/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows'] = $response->total;
			$config['uri_segment'] = 7;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['error'] = '';
			}else{
				$data['error'] = 1;
				$data['business_all_businesses'] = '';
			}
			//echo '<pre>';print_r($data);exit;
			$data['sort_order'] = $sort_order;
			$data['order_by'] = $order_by;
			$data['query_id'] = $query_id;
			$data['search_by'] = $search_by;
			$data['search_by_value'] = $search_by_value;
			$data['business_fields'] = array('business_name'=>'Business Name', 'url'=>'Web Site', 'category_name'=>'Category', 'start_date'=>'Start Date', 'status'=>'Status', 'created_date'=>'Date');
			//$data['member_fields'] = array(''=>'Undefined', 1=>'Member', 0=>'Non-Member');
			//echo '<pre>';print_r($data['business_fields']);exit;
			$this->load->view('admin/businesses', $data);
 		}
		
		function search_by(){
			if($this->input->post('search') == ''){
				$search = $this->input->post('search_date');
			}else{
				$search = $this->input->post('search');	
			}
				
			$query_array = array(
				'search_by'=>$this->input->post('search_by'),
				'search'=>$search
			);
			$query_id=rand();
			$_SESSION['search']=$query_array;
			//echo '<pre>';print_r($_SESSION['search']);exit;
			redirect('admin/businesses/index/'.$query_id);
			
		}
		
	/* 	function create_businesses_page(){
			$url = "http://localhost/azindia/azindia-api/json.api";
				$data = array('www-command'=>'business-get_businesscategory_list');
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				curl_close($ch);
				$data['get_businesscategory_list'] = $response;
				$this->load->view('admin/create_businesses_page', $data);
		} */
		
		function create_businesses(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('name', 'Name', 'required');
				$this->form_validation->set_rules('category', 'Category', 'required|callback_select');
				$this->form_validation->set_rules('type', 'Business Type', 'required|callback_select');
				$this->form_validation->set_rules('start', 'Start Date', 'required');
				$this->form_validation->set_rules('description', 'Description', 'required|max_length[900]');
				$this->form_validation->set_rules('address', 'Address', 'required|max_length[1200]');
				$this->form_validation->set_rules('image', 'Image');
				$this->form_validation->set_rules('url', 'URL', 'callback_valid_url_format');
				
					if($this->form_validation->run() == FALSE){
						
						$url = "http://localhost/azindia/azindia-api/json.api";
				$data = array('www-command'=>'business-get_businesscategory_list');
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				curl_close($ch);
				$data['get_businesscategory_list'] = $response;
				$this->load->view('admin/create_businesses_page', $data);
				}else{
					
					
					$tmp_name = $_FILES['image']['tmp_name'];
					$image = addslashes($tmp_name);
					$image = file_get_contents($image);
					$image = base64_encode($image);
					//echo '<pre>';print_r($image);exit;
			
					$url = CURL_URL;
					$data = array('www-command'=>'business-create_business', 
					'business_name' => $this->input->post('name'),
					'businesscat_id' => $this->input->post('category'),
					'member' => $this->input->post('member'),
					'business_type' => $this->input->post('type'),
					'start_date' => $this->input->post('start'),
					'description' => $this->input->post('description'),
					'url' => $this->input->post('url'),
					'address' => $this->input->post('address'),
					'business_image' => $image,
					'image_name' => $_FILES['image']['name']
					);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/businesses/index/');
				}
			}
		
		function valid_url_format($url){
			if($url == ''){
					return true;
				}
			$pattern = '|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i';
			if(!preg_match($pattern, $url)){
				$this->form_validation->set_message('valid_url_format', 'The URL you entered is not accessible.');
				return false;
				}
				return true;
			}
			
			function select($select)
				{
				 	if($select == 'select' || $select == 'select Business Type')
					{ 
					  $this->form_validation->set_message('select', 'Please select.');
					  return FALSE;
					}
				 return TRUE;
				 
				}
			
			function edit_businesses(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('name', 'Name', 'required');
				$this->form_validation->set_rules('category', 'Category', 'required');
				$this->form_validation->set_rules('type', 'Business Type', 'required|callback_select');
				$this->form_validation->set_rules('start', 'Start Date', 'required');
				$this->form_validation->set_rules('description', 'Description', 'required|max_length[900]');
				$this->form_validation->set_rules('address', 'Address', 'required|max_length[1200]');
				$this->form_validation->set_rules('image', 'Image');
				$this->form_validation->set_rules('url', 'URL', 'required|callback_valid_url_format');
				
				if($this->form_validation->run() == FALSE){
						$this->load->model('admin_business_model');
						if($this->uri->segment(4) > 0){
					$_SESSION['business_id'] = $this->uri->segment(4);
				}
				$data = array('www-command'=>'business-get_businesscategory_list');
				$get_businesscategory_list = $this->admin_business_model->business_list($data);
				$data = array('www-command'=>'business-get_business', 'business_id'=>$_SESSION['business_id']);
				$get_business = $this->admin_business_model->business_list($data);
				
				$data['get_businesscategory_list'] = $get_businesscategory_list;
				$data['get_business'] = $get_business;
				$data['member_fields'] = array(''=>'Undefined', 1=>'Member', 0=>'Non-Member');
				//echo '<pre>';print_r($data);exit;
				$this->load->view('admin/edit_businesses_page', $data);
			
			}else{
			
					$tmp_name = $_FILES['image']['tmp_name'];
					$image = addslashes($tmp_name);
					$image = file_get_contents($image);
					$image = base64_encode($image);
					//echo '<pre>';print_r($image);exit;
						
					$url = CURL_URL;
					$data = array('www-command'=>'business-edit_business', 
					'business_name' => $this->input->post('name'),
					'businesscat_id' => $this->input->post('category'),
					'member' => $this->input->post('member'),
					'business_type' => $this->input->post('type'),
					'start_date' => $this->input->post('start'),
					'description' => $this->input->post('description'),
					'url' => $this->input->post('url'),
					'address' => $this->input->post('address'),
					'business_image' => $image,
					'image_name' => $_FILES['image']['name'],
					'business_id' => $_SESSION['business_id']
					);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/businesses/index/');
				}
			}
			
			function delete_businesses(){
				$url = CURL_URL;
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'business-delete_business','status'=>$status, 'business_id'=>$this->uri->segment(4));
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				
				redirect('admin/businesses/index/');
			}
}